package com.ygou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ygou.pojo.Browfav;
import com.ygou.pojo.GoodsWithBLOBs;
import com.ygou.pojo.Users;
import com.ygou.service.BrowfavService;
import com.ygou.service.GoodService;

@Controller
@RequestMapping(value = "/browfav")
@SessionAttributes(value = "")
public class BrowfavController {
	@Autowired
	BrowfavService browfavService;
	@Autowired
	GoodService goodService;

	@RequestMapping(value = "/selectByExample",produces="application/json;charset=UTF-8")
	public @ResponseBody Map<String,Object> selectByExample(ModelMap model,Integer goodsId, HttpSession session) {
		GoodsWithBLOBs good = goodService.selById(goodsId);
		Users user = (Users) session.getAttribute("user");
		Map<String, Object>  map=new HashMap<String,Object>();
		if (user != null) {
			Browfav brow = browfavService.selectByExample(user.getUserid(), goodsId,2);
			if (brow == null || "".equals(brow)) {
				brow = new Browfav();
				brow.setUserid(user.getUserid());
				brow.setGoodsid(goodsId);
				brow.setGoodsnames(good.getGoddstitle());
				brow.setGoodsprice(good.getTrueprice());
				brow.setGoodsthums(good.getGoodsthums());
				brow.setLasttime(new java.util.Date());
				brow.setCreatetime(new java.util.Date());
				brow.setBrowfavtype((byte)2);
				good.setCollectnum(good.getCollectnum()+1);
				goodService.doUpdate(good);
				map.put("results", browfavService.insert(brow));
				map.put("num",good.getCollectnum());
				map.put("addclass","item");
				map.put("remclass","fav-i");
			} else {
				good.setCollectnum(good.getCollectnum()-1);
				goodService.doUpdate(good);
				map.put("results",browfavService.deleteByPrimaryKey(brow.getId()));
				map.put("num",good.getCollectnum());
				map.put("addclass","fav-i");
				map.put("remclass","item");
			}
		}
		return map;
	}
	
	
	
	
	/**********/
	//显示足迹
	@RequestMapping(value="/show")
	public String showDetail(int userid,ModelMap model) {
		List<Browfav> ls1 = browfavService.selectByExampleById(userid);
		model.put("brows", ls1);
		return "footprint";
	}
	
	//显示收藏
	@RequestMapping(value="/detil")
	public String showCollection(int userid,ModelMap model){
		List<Browfav> brow = browfavService.selectByWhere(userid);
		model.put("brow", brow);
		return "mylike";
	}
}
